<!-- 修改组织架构 -->
<template>
  <dt-modal-edit ref='info' :title='title' :label-width='100' okText='确定' :width='60'      @cancelData="cancelData"
    @ok='onOk' :okDisabled='OKFlag' :transfer='true'>
    <div class='change_organiza'>
      <div slot="bottom">
        <div class="target_total">
          <div class="target_mouth">
            <span>人员姓名：</span><span>{{infoData.name}}({{infoData.workNo}})</span>
            <span style="margin-left:20px;">人员角色：</span><span>{{roleData.name}}</span>

          </div>
          <div class="target_mouth second_line">
            <span>对应组织：</span>
            <div class="orgBox">
              <span v-for="(item,index) in roleData.orgList" :key='index'>
                {{item.name}}
              </span>
            </div>
          </div>
        </div>
      </div>
      <div class="tree_warp">
        <div class="tree_top_txt">
          请选择权限所对应的组织：
        </div>
        <Tree :data="treeList" @on-select-change="treeChange" class="tree_render"></Tree>
      </div>
    </div>
  </dt-modal-edit>
</template>

<script>
  import organApi from '@/biz/option/businessOrgan'
  import userOrg from '@/biz/backStage/organizationManagement'
  import organizationManagementMgr from '@/biz/organizationManagement/organizationManagement'
  export default {
    components: {},
    data() {
      return {
        roleData: '',
        title: '添加人员组织',
        infoData: {
          userInfo: {},
          orgInfo: {},
          treeTitle: '',
        },
        checkOrgIds: [],
        treeList: [],
        curTreeId: '',
        OKFlag: true,
        chooseOrg: {}
      }
    },
    methods: {
      cancelData(){
        this.checkOrgIds = []
      },
      show(data, val, valAll) {
        console.log(data, val, valAll, '传过去的人员信息以及当前角色信息');
        this.checkOrgIds = []
        this.infoData = data
        this.roleData = val.roleVO
        valAll &&
          valAll.forEach((item) => {
            this.checkOrgIds.push(item.id)
          })

        this.$refs.info.show()


        this.getBusinessList()
      },


      hide() {
        this.chooseOrg = {}
        this.$refs.info.hide()
      },
      onOk() {
        if (JSON.stringify(this.chooseOrg) == "{}") {
          this.$Message.warning('请先选择一个组织！')
          return false
        }
        this.$emit('chooseOrgEvent', this.chooseOrg)
        this.hide()
        // let params = {
        //   userId: this.infoData.userInfo.userId,
        //   orgId: this.curTreeId
        // }
        // userOrg.addUserOrg(params).then(res => {
        //   this.$Message.success('添加成功')
        //   this.$emit('success', res)
        //   this.hide()
        // })
      },
      //循环遍历部门
      forEachDepartment(mess) {
        mess.map((item) => {
          item.title = item.orgName
          item.expand = true
          item.render = this.renderContent()
          item.disabled = this.checkOrgIds.includes(item.id) ? true : false
          item.children && this.forEachDepartment(item.children)
        })
      },
      // 业务架构
      getBusinessList() {
        organizationManagementMgr
          .getOrg()
          .then((res) => {
            // this.recursion(res)
            // this.data1 = res
            if (res) {
              res.forEach((ms) => {
                ms.title = ms.orgName
                ms.expand = true
                ms.render = this.renderContent()
                ms.disabled = this.checkOrgIds.includes(ms.id) ? true : false
                ms.children && this.forEachDepartment(ms.children)
              })
              this.treeList = res
            }
          })

          .catch((err) => {})
      },
      //获取部门列表
      // getDepartmentList() {
      //   organApi.getWebOrg().then((res) => {

      //     if (res) {
      //       res.forEach((ms) => {
      //         ms.title = ms.orgName
      //         ms.expand = true
      //         ms.render = this.renderContent()
      //         ms.disabled = this.checkOrgIds.includes(ms.orgId) ? true : false
      //         ms.children && this.forEachDepartment(ms.children)
      //       })
      //       this.treeList = res
      //     }
      //   })
      // },
      treeChange(event) {
        console.log(event, 'tree选中的');
        if (event[0]) {
          this.OKFlag = false
          // this.curTreeId = event[0] ? event[0].orgId : ''
          this.chooseOrg = { id: event[0].id, name: event[0].orgName }
        } else {
          this.OKFlag = true
        }
      },
      renderContent() {
        return (h, {
          data
        }) => {
          return h('span', {
            style: {
              display: 'inline-block',
              backgroundColor: data.disabled ? '#f7f7f7' : '',
              color: data.disabled ? '#c5c8ce' : '',
              padding: '0 4px',
              lineHeight: '1.5'
            },
          }, data.title)
        }
      },
    },
  }
</script>
<style lang='scss' scoped>
  .change_organiza {
    .target_total {
      background-color: rgb(253 251 232);
      padding: 15px;
      margin-bottom: 15px;
      font-size: 16px;
      line-height: 30px;
    }

    .tree_warp {
      .tree_top_txt {
        font-size: 16px;
      }

      .tree_render {
        height: 500px;
        overflow: auto;
      }
    }

    .second_line {
      display: flex;
    }

    .orgBox {
      display: flex;
      flex-direction: column;
    }


  }
</style>